package com.windystory.wslibrary.ui.webview;

import android.content.Context;
import android.view.View;
import android.widget.ProgressBar;

import com.tencent.smtt.export.external.interfaces.SslError;
import com.tencent.smtt.export.external.interfaces.SslErrorHandler;
import com.tencent.smtt.sdk.WebChromeClient;
import com.tencent.smtt.sdk.WebSettings;
import com.tencent.smtt.sdk.WebViewClient;

/**
 * Created by you on 2017/3/18.
 * js交互类
 */

public class InitWebViewAndCallBack {

    private WebView mWebView;

    public InitWebViewAndCallBack(WebView mWebView, Context c) {
        this.mWebView = mWebView;
    }

    public static void initWebView(final WebView mWebView, final ProgressBar mProgress, final Context c) {
        mWebView.getSettings().setJavaScriptEnabled(true);
        InitWebViewAndCallBack jsCallback = new InitWebViewAndCallBack(mWebView, c);
        mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        mWebView.getSettings().setSavePassword(false);

        mWebView.getSettings().setDomStorageEnabled(true);
        mWebView.getSettings().setDatabaseEnabled(true);
        mWebView.getSettings().setAppCacheMaxSize(1024 * 1024 * 8);

        mWebView.getSettings().setAllowFileAccess(true);
        mWebView.getSettings().setAppCacheEnabled(true);

        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public void onReceivedError(com.tencent.smtt.sdk.WebView view, int errorCode, String description, String failingUrl) {
                super.onReceivedError(view, errorCode, description, failingUrl);
                setErrorPage(view);
            }

            @Override
            public void onReceivedSslError(com.tencent.smtt.sdk.WebView webView, SslErrorHandler sslErrorHandler, SslError sslError) {
                super.onReceivedSslError(webView, sslErrorHandler, sslError);
                sslErrorHandler.proceed();
            }
        });

        mWebView.clearCache(true);
        mWebView.clearHistory();
        mWebView.setHapticFeedbackEnabled(false);


        mWebView.requestDisallowInterceptTouchEvent(true);


        mWebView.setWebChromeClient(new WebChromeClient() {

            @Override
            public void onProgressChanged(com.tencent.smtt.sdk.WebView webView, int newProgress) {
                super.onProgressChanged(webView, newProgress);
                if (newProgress < 0) {
                    mProgress.setVisibility(View.VISIBLE);
                    mProgress.setProgress(0);
                    return;
                }
                if (newProgress == 100) {
                    mProgress.setVisibility(View.GONE);
                } else {
                    mProgress.setVisibility(View.VISIBLE);
                    mProgress.setProgress(newProgress);
                }
            }


        });
    }

    /**
     * 错误页
     *
     * @param view
     */
    private static void setErrorPage(com.tencent.smtt.sdk.WebView view) {
        view.loadUrl("file:///android_asset/load_error_empty.html");
        view.setVisibility(View.GONE);
    }


}
